#include <avr/io.h> 
#include <avr/interrupt.h> 

#define MAXSENSOR 6
volatile int currentSensor;
volatile int readcount=0;


void setup()
{
  Serial.begin(115200);
  Serial.flush();

  
}

void loop()
{
  Serial.flush();
  Serial.println("Type any character to start");
  while (!Serial.available());

  initADC();

}

void initADC()
{
  currentSensor=0;
  ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0);
  ADMUX = (1<<REFS0)|(1<<ADLAR); //use AVCC as reference, set to left adjusted, select ADC0 
  ADCSRA = (1<<ADEN)|(1<<ADIE)|(1<<ADATE); //enable ADC, ADC interrupt and ADC auto trigger 
  ADCSRA = (1<<ADEN)|(1<<ADIE); //enable ADC, ADC interrupt and ADC auto trigger 
  //Because ADCSRB is not set and the ADATE is set, the ADC is in free running mode 
  ADCSRA |= _BV(ADSC);

}



ISR(ADC_vect) 
{ 
  if(readcount>0)
  {
    Serial.print("A");
    Serial.print(currentSensor);
    Serial.print("=");
    byte low=ADCL;
    byte high=ADCH;
    uint16_t val=high<<8|low;
    Serial.print(val,DEC);
    readcount=0;
    currentSensor++;
    if(currentSensor>=MAXSENSOR)
    {
      Serial.println();
      currentSensor=0; 
      return;
    }
  }
  else
  {
    readcount++; 
  }

  ADMUX = (1<<REFS0)|(1<<ADLAR)|currentSensor;
  ADCSRA |= (1<<ADSC); 
}


